package com.hex.tcpproxy.process;

import com.hex.tcpproxy.util.ProxyClient;
import com.hex.tcpproxy.util.ProxyHandler;
import com.hex.tcpproxy.util.ProxyServer;

/**
 * @author: [wujie]
 * @createTime: [2019/10/9 9:17]
 * @description:
 */
public class FakeClient implements Runnable {
    private MainClient mainClient;
    private Integer port;
    private ProxyHandler proxyHandler;
    private ProxyClient proxyClient;
    private String host;
    private String address;


    public FakeClient(String address, MainClient mainClient) {
        this.address = address;
        String[] split = address.split(":");
        this.host = split[0];
        this.port = Integer.parseInt(split[1]);
        this.mainClient = mainClient;
    }

    @Override
    public void run() {
        proxyHandler = new ProxyHandler() {
            @Override
            public void read(String addr, byte[] bytes) {
                mainClient.writeBack(address, bytes);
            }
        };
        proxyClient = new ProxyClient(host, port, proxyHandler);
        proxyClient.connect();
    }

    public void close() {
        proxyClient.close();
    }
}
